#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    bool isHappy(int n)
    {
        unordered_set<int> set;
        while (n != 1)
        {
            //cout << n << endl;
            if (set.find(n) != set.end())
            {
                return false;
            }
            set.insert(n);
            string str = to_string(n);
            n = 0;
            for (auto ch : str)
            {
                int num = ch - '0';
                n += num * num;
            }
        }
        return true;
    }
};

int main()
{
    Solution sol;
    cout << sol.isHappy(19) << endl;
    cout << sol.isHappy(2) << endl;
    return 0;
}